package com.example.coolweatherreview.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.example.coolweatherreview.bean.Comf;
import com.example.coolweatherreview.bean.Suggestion;
import com.example.coolweatherreview.bean.Tmp;
import com.example.coolweatherreview.bean.WeatherData;
import com.example.coolweatherreview.model.City;
import com.example.coolweatherreview.model.CoolWeatherDB;
import com.example.coolweatherreview.model.County;
import com.example.coolweatherreview.model.Province;
import com.google.gson.Gson;

public class Utility {
	
	/**
	 * 解析服务器返回的省级信息
	 * @param coolWeatherDB
	 * @param response
	 * @return
	 */
	public synchronized static boolean handleProvinceResponse(CoolWeatherDB coolWeatherDB,String response){
		if(!TextUtils.isEmpty(response)){
			String []allProvince = response.split(",");
			if(allProvince!=null&allProvince.length>0){
				for(String p:allProvince){
					String[] array = p.split("\\|");
					Province province = new Province();
					province.setProvinceCode(array[0]);
					province.setProvinceName(array[1]);
					//将解析出的数据存入Province表
					coolWeatherDB.saveProvince(province);
				}
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 解析服务器返回的城市信息
	 * @param coolWeatherDB
	 * @param response
	 * @return
	 */
	public synchronized static boolean handleCityResponse(CoolWeatherDB coolWeatherDB,String response,int provinceId){
		if(!TextUtils.isEmpty(response)){
			String[] allCities = response.split(",");
			if(response!=null && response.length()>0){
				for(String c: allCities){
					String []array = c.split("\\|");
					City city = new City();
					city.setCityCode(array[0]);
					city.setCityName(array[1]);
					city.setProvinceId(provinceId);
					coolWeatherDB.saveCity(city);
				}
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 解析服务器返回的县级信息
	 * @param coolWeatherDB
	 * @param response
	 * @param cityId
	 * @return
	 */
	public synchronized static boolean handleCountyResponse(CoolWeatherDB coolWeatherDB,String response,int cityId){
		if(!TextUtils.isEmpty(response)){
			String []allCounties = response.split(",");
			if(allCounties!=null && allCounties.length>0){
				for(String c:allCounties){
					String []arr = c.split("\\|");
					County county = new County();
					county.setCountyCode(arr[0]);
					county.setCountyName(arr[1]);
					county.setCityId(cityId);
					coolWeatherDB.saveCounty(county);
				}
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 解析服务器返回的json数据
	 * @param context
	 * @param response
	 */
	public static void handleWeatherResponse(Context context,String response){
		Gson gson = new Gson();
		WeatherData weatherData = gson.fromJson(response, WeatherData.class);
		//城市名称
		String cityName = weatherData.getHeWeatherdataservice().get(0).getBasic().getCity();
		//城市代码
		String weatherCode = weatherData.getHeWeatherdataservice().get(0).getBasic().getId();
		//描述时间
		String publishTime = weatherData.getHeWeatherdataservice().get(0).getBasic().getUpdate().getLoc();
		//即时天气
		String currentTemp = weatherData.getHeWeatherdataservice().get(0).getNow().getTmp();
		//即时天气图片
		String currentPicUrl = weatherData.getHeWeatherdataservice().get(0).getNow().getCond().getCode();
		//风俗
		String wind = weatherData.getHeWeatherdataservice().get(0).getNow().getWind().getDir();
		//天气描述
		String weatherDesc = weatherData.getHeWeatherdataservice().get(0).getNow().getCond().getTxt();
		//提醒消息
		Suggestion sug = weatherData.getHeWeatherdataservice().get(0).getSuggestion();
		Comf comf = sug.getComf();
		String notification = comf.getTxt();
		//未来温度和图片
		String next1Temp1 = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(1).getTmp().getMin();
		String next1Temp2 = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(1).getTmp().getMax();
		String next1Pic = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(1).getCond().getCode_d();
		
		
		String next2Temp1 = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(2).getTmp().getMin();
		String next2Temp2 = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(2).getTmp().getMax();
		String next2Pic = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(2).getCond().getCode_d();
		
		
		String next3Temp1 = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(3).getTmp().getMin();
		String next3Temp2 = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(3).getTmp().getMax();
		String next3Pic = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(3).getCond().getCode_d();
		
		
		String next4Temp1 = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(4).getTmp().getMin();
		String next4Temp2 = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(4).getTmp().getMax();
		String next4Pic = weatherData.getHeWeatherdataservice().get(0).getDaily_forecast().get(4).getCond().getCode_d();
		
		saveWeatherInfo(context,cityName,weatherCode,currentTemp,currentPicUrl,wind,weatherDesc,notification,
				next1Temp1,next1Temp2,next2Temp1,next2Temp2,next3Temp1,next3Temp2,next4Temp1,next4Temp2,next1Pic,next2Pic,next3Pic,
				next4Pic);
	}
	
	/**
	 * 将服务器返回的所有信息保存到SharedPreference中
	 * @param context
	 * @param cityName
	 * @param weatherCode
	 * @param currentTemp
	 * @param wind
	 * @param weatherDesc
	 * @param notification
	 * @param next1Temp1
	 * @param next1Temp2
	 * @param next2Temp1
	 * @param next2Temp2
	 * @param next3Temp1
	 * @param next3Temp2
	 * @param next4Temp1
	 * @param next4Temp2
	 */
	private static void saveWeatherInfo(Context context, String cityName,
			String weatherCode, String currentTemp,String currentPicUrl, String wind,
			String weatherDesc, String notification, String next1Temp1,
			String next1Temp2, String next2Temp1, String next2Temp2,
			String next3Temp1, String next3Temp2, String next4Temp1,
			String next4Temp2,String next1Pic,String next2Pic,String next3Pic,String next4Pic) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日",Locale.CHINA);
		SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
		editor.putBoolean("city_selected", true);
		editor.putString("city_name", cityName);
		editor.putString("weather_code", weatherCode);
		editor.putString("current_temp", currentTemp);	
		editor.putString("currentPicUrl", currentPicUrl);
		editor.putString("wind", wind);		
		editor.putString("weather_desc", weatherDesc);		
		editor.putString("notification", notification);		
		editor.putString("next1Temp1", next1Temp1);
		editor.putString("next1Temp2", next1Temp2);
		editor.putString("next2Temp1", next2Temp1);
		editor.putString("next2Temp2", next2Temp2);
		editor.putString("next3Temp1", next3Temp1);
		editor.putString("next3Temp2", next3Temp2);		
		editor.putString("next4Temp1", next4Temp1);
		editor.putString("next4Temp2", next4Temp2);
		
		editor.putString("next1Pic", next1Pic);
		editor.putString("next2Pic", next2Pic);
		editor.putString("next3Pic", next3Pic);
		editor.putString("next4Pic", next4Pic);
		
		editor.putString("current_date", sdf.format(new Date()));
		editor.commit();
	}
	
//	/**
//	 * 
//	 * @param context
//	 * @param cityName
//	 * @param weatherCode
//	 * @param temp1
//	 * @param weatherDesc
//	 * @param publishTime
//	 */
//	private static void saveWeatherInfo(Context context, String cityName,
//			String weatherCode, String temp1, String weatherDesc,
//			String publishTime) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日",Locale.CHINA);
//		SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
//		editor.putBoolean("city_selected", true);
//		editor.putString("city_name", cityName);
//		editor.putString("weather_Code", weatherCode);
//		editor.putString("temp1", temp1);
//		editor.putString("weather_Desc", weatherDesc);
//		editor.putString("publish_time", publishTime);
//		editor.putString("current_date", sdf.format(new Date()));
//		editor.commit();
//	}

	
}
















